package com.ruoyi.web.controller.fireman;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.fireman.domain.TMenu;
import com.ruoyi.fireman.service.ITMenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fireman.domain.TArticle;
import com.ruoyi.fireman.service.ITArticleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文章Controller
 *
 * @author ruoyi
 * @date 2021-08-31
 */
@Controller
@RequestMapping("/fireman/article")
public class TArticleController extends BaseController {
    private String prefix = "fireman/article";

    @Autowired
    private ITArticleService tArticleService;
    @Autowired
    private ITMenuService itMenuService;

    @RequiresPermissions("fireman:article:view")
    @GetMapping()
    public String article() {
        return prefix + "/article";
    }

    /**
     * 查询文章列表
     */
    @RequiresPermissions("fireman:article:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(String title, String content, Long tMenuId, Long type_1) {
        startPage();
        Map param = new HashMap();
        if (StringUtils.isNotEmpty(title)) {
            param.put("title", title);
        }
        if (StringUtils.isNotEmpty(content)) {
            param.put("content", content);
        }
        if (tMenuId != null) {
            param.put("tMenuId", tMenuId);
        }
        if (type_1 != null) {
            param.put("type_1", type_1);
        }
        List<TArticle> list = tArticleService.selectTArticleListByMap(param);
        return getDataTable(list);
    }

    /**
     * 导出文章列表
     */
    @RequiresPermissions("fireman:article:export")
    @Log(title = "文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TArticle tArticle) {
        List<TArticle> list = tArticleService.selectTArticleList(tArticle);
        ExcelUtil<TArticle> util = new ExcelUtil<TArticle>(TArticle.class);
        return util.exportExcel(list, "文章数据");
    }

    /**
     * 新增文章
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存文章
     */
    @RequiresPermissions("fireman:article:add")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TArticle tArticle) {
        return toAjax(tArticleService.insertTArticle(tArticle));
    }

    /**
     * 修改文章
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        TArticle tArticle = tArticleService.selectTArticleById(id);
        mmap.put("tArticle", tArticle);
        return prefix + "/edit";
    }

    /**
     * 修改保存文章
     */
    @RequiresPermissions("fireman:article:edit")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TArticle tArticle) {
        return toAjax(tArticleService.updateTArticle(tArticle));
    }

    /**
     * 删除文章
     */
    @RequiresPermissions("fireman:article:remove")
    @Log(title = "文章", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tArticleService.deleteTArticleByIds(ids));
    }

    /**
     * 获取文章分类
     */
    @GetMapping("/menus")
    @ResponseBody
    public JSONArray menus() {
        List<TMenu> tMenus = itMenuService.selectOneTMenuList(null);
        JSONArray jsonArray = JSONObject.parseArray(JSON.toJSONString(tMenus));
        return jsonArray;
    }
}
